
package org.beaconframework.ext.ibm.wps.ui;

import org.beaconframework.ext.ibm.wps.data.GroupsQueryTemplate;
import org.beaconframework.ext.ibm.wps.data.TaskQueryTemplate;
import org.beaconframework.ext.ibm.wps.data.UserQueryTemplate;
import org.beaconframework.ext.rs.RestXmlItemContainer;
import org.beaconframework.vaadin.ui.DynamicComboBox;

import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Table;
import com.vaadin.ui.AbstractSelect.Filtering;

public class WpsComponentBuilder {

    public Table createGroupsTable() {
        final Table table = new Table() {

            @Override
            public void attach() {
                // TODO Auto-generated method stub
                super.attach();
                ((RestXmlItemContainer) getContainerDataSource()).refresh();
            }

        };
        table.setImmediate(true);
        RestXmlItemContainer c = new RestXmlItemContainer();
        c.setRowXPath("/root/resourceList");
        c.addXPathProperty("cn", ".//cn/attributeValue/text()");
        c.addXPathProperty("description",
                ".//description/attributeValue/text()");

        table.setContainerDataSource(c);

        c.setQueryTemplate(new GroupsQueryTemplate());
        table.setSizeFull();
        table.setSelectable(true);
        ValueChangeListener vcl = new Property.ValueChangeListener() {

            /**
             * 
             */
            private static final long serialVersionUID = -7712514419224837501L;


            public void valueChange(Property.ValueChangeEvent event) {
                // getWindow().showNotification("You clicked on: "+event);
                Property p = event.getProperty();

                Object id = p.getValue();
                if (id != null) {
                    Item item = table.getItem(id);
                    table.getWindow().showNotification(
                            "You clicked on: " + item.getItemProperty("cn"));
                }

            }
        };

        ItemClickListener icl = new ItemClickListener() {

            /**
             * 
             */
            private static final long serialVersionUID = -8125033939153827852L;


            public void itemClick(ItemClickEvent event) {
                table
                        .getWindow()
                        .showNotification(
                                "Click",
                                "You clicked on: "
                                        + event.getItem().getItemProperty("cn")
                                                .getValue(),
                                com.vaadin.ui.Window.Notification.TYPE_TRAY_NOTIFICATION);

            }
        };
        table.addListener(icl);
        return table;
    }


    public Table createProcessInstanceTable() {
        Table table = new Table();
        RestXmlItemContainer c = new RestXmlItemContainer();

        c.setRowXPath("/root/items");
        c.addXPathProperty("name", "name/text()");
        c.addXPathProperty("displayName", "displayName/text()");
        c.addXPathProperty("name", "name/text()");

        /*
         * addSimpleProperty("description", "Description");
         * addSimpleProperty("processTemplateName", "Process Template Name");
         * 
         * addSimpleProperty("executionState", "Execution State");
         * addSimpleProperty("topLevelProcessInstanceName",
         * "Top Level Process Instance Name"); addSimpleProperty("creationTime",
         * "Creation Time");
         * 
         * addSimpleProperty("starter", "Starter");
         * addSimpleProperty("topLevelProcessInstanceID",
         * "Top Level Process Instance ID"); addSimpleProperty("startTime",
         * "Start Time"); addSimpleProperty("processTemplateID",
         * "Process Template ID");
         * addSimpleProperty("parentProcessInstanceName",
         * "Parent Process Instance Name"); addSimpleProperty("completionTime",
         * "Completion Time"); addSimpleProperty("parentProcessInstanceID",
         * "Parent Process Instance ID"); addSimpleProperty("piid", "PIID");
         * addSimpleProperty("resumptionTime", "Resumption Time");
         * c.setQueryTemplate(new ProcessInstanceQueryTemplate());
         */
        table.setContainerDataSource(c);

        table.setSizeFull();
        return table;
    }


    public Table createTaskTable() {
        Table table = new Table();
        RestXmlItemContainer c = new RestXmlItemContainer();
        c.setRowXPath("/root/items");
        c.addXPathProperty("tkiid", "TKIID/text()");
        c.addXPathProperty("description", "Description/text()");
        /*
         * addSimpleProperty("originator", "Originator");
         * addSimpleProperty("displayName", "Display Name");
         * addSimpleProperty("owner", "Owner"); addSimpleProperty("name",
         * "Name"); addSimpleProperty("priority", "Priority");
         * 
         * addSimpleProperty("activationTime", "Activation Time");
         * addSimpleProperty("starter", "Starter");
         * addSimpleProperty("positionInHierarchy", "Position In Hierarchy");
         * addSimpleProperty("isWaitingForSubTask", "Is Waiting For Subtask");
         * addSimpleProperty("dueTime", "Due Time");
         * addSimpleProperty("startTime", "Start Time");
         * addSimpleProperty("kind", "Kind");
         * addSimpleProperty("durationUntilExpires", "Duration Until Expiry");
         * addSimpleProperty("type", "Type");
         * addSimpleProperty("taskTemplateID", "Task Template ID");
         * addSimpleProperty("state", "State");
         * addSimpleProperty("firstActivationTime", "First Activation Time");
         */
        c.setQueryTemplate(new TaskQueryTemplate());

        table.setContainerDataSource(c);

        table.setSizeFull();

        // removeContainerProperty("tkiid");
        // setContainerDataSource(c);
        // setColumnHeader("tkiid", "Test");
        /*
         * Iso8601DateFormatFunction ff = new Iso8601DateFormatFunction();
         * ff.setOutputTimeZone(TimeZone.getTimeZone("EST"));
         * c.getCustomResolver().registerNamespace("b",
         * "http://www.haddonhillgroup.com/beacon/");
         * c.getCustomResolver().registerFunction("b", "format-date", new
         * Iso8601DateFormatFunction());
         * 
         * c .addXPathContainerProperty("firstActivationTime",
         * "b:format-date(firstActivationTime/text(),'yyyy-MM-ddHH:mm:ssZ')");
         */

        return table;
    }


    public Table createUsersTable() {
        Table t = new Table() {
            @Override
            public void attach() {
                ((RestXmlItemContainer) getContainerDataSource())
                        .queryCriteriaChanged("WILL-NOT-FIND");
            }
        };
        RestXmlItemContainer c = new RestXmlItemContainer();
        c.setQueryTemplate(new UserQueryTemplate());
        c.setRowXPath("/root/resourceList[profile/type/text()='user']");
        t.setSizeFull();
        c.addXPathProperty("uid", "uid/text()");
        c.addXPathProperty("cn", "cn/text()");
        c.addXPathProperty("sn", "sn/text()");
        c.addXPathProperty("mail", "mail/text()");

        t.setContainerDataSource(c);
        return t;
    }


    public ComboBox createUserSelectionComboBox() {

        DynamicComboBox cb = new DynamicComboBox();
        RestXmlItemContainer c = new RestXmlItemContainer();

        String mp = "profile[type/text()=\"user\"]/*/uid/attributeValue[1]/text()";

        c.setRowXPath("/*/resourceList");

        c.addXPathProperty("name", mp);

        cb.setContainerDataSource(c);
        cb.setItemCaptionPropertyId("name");
        c.setQueryTemplate(new UserQueryTemplate());

        cb.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS);
        cb.setContainerDataSource(c);
        // c.queryCriteriaChanged("INVALIDINPUT");

        return cb;
    }
}
